package com.ccsu.eb.config;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ccsu.eb.mapper.EbProductCategoryMapper;
import com.ccsu.eb.model.EbProductCategory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import java.util.List;

/**
 * @Description: 此类是在spring boot项目启动后自动 调用run方法，进行项目的 一些初始化工作！！
 * @Auther: 新梦想*陈超
 * @Date: 2022/3/18 08:48
 */
@Component
public class SpringInit  implements CommandLineRunner {
    @Resource
    private EbProductCategoryMapper  ebProductCategoryMapper;

    @Resource
    private ServletContext application;

    @Override
    public void run(String... args) throws Exception {
        System.out.println("我是项目启动后自动执行的方法.....");
        //TODO 1.查询大分类 和小分类集合
        List<EbProductCategory> bigCateList = ebProductCategoryMapper.selectList(new QueryWrapper<EbProductCategory>()
                .apply("epc_id=epc_parent_id"));
        List<EbProductCategory> smallCateList = ebProductCategoryMapper.selectList(new QueryWrapper<EbProductCategory>()
                .apply("epc_id!=epc_parent_id"));
        //TODO 2.将这个分类数据加载application域中
        application.setAttribute("bigCateList",bigCateList);
        application.setAttribute("smallCateList",smallCateList);
    }
}
